import { fetchHome } from '../../services/home/home';
import { fetchReviewsList } from '../../services/reviews/fetchReviews';

const app = getApp();

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    defaultKeyword: '食堂上新啦！',
    keyword: '',
    reviewsList: [],
    reviewsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'scaleToFill' },
  },

  privateData: {
    tabIndex: 0,
    page_num: 0,
    page_size: 10,
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    app.setGlobalData({
      home_paginate_num: 20,
      home_paginate_index: 0,
    });
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadReviewsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({ swiper, tabList }) => {
      this.setData({
        tabList,
        // imgSrcs: swiper,
        pageLoading: false,
        reviewsListLoadStatus: 1,
      });

      this.privateData.page_size = 10;
      this.privateData.page_num = 0;

      this.loadReviewsList(true);
    });

    // console.log(this.data.reviewsList);
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.privateData.page_size = 10;
    this.privateData.page_num = 0;
    this.setData({
      reviewsListLoadStatus: 1,
    })
    this.loadReviewsList(true);
  },

  onReTry() {
    this.loadReviewsList();
  },

  async loadReviewsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    if (this.data.reviewsListLoadStatus === 2) {
      return ;
    }

    this.setData({ reviewsListLoadStatus: 1 });

    const pageSize = this.privateData.page_size

    let pageIndex = this.privateData.tabIndex * pageSize + this.privateData.page_num + 1;

    if (fresh) {
      pageIndex = 1;
    }

    try {
      let has_status = 0;
      const nextList = await fetchReviewsList(pageIndex, pageSize).then((data) => {
        if (!data.has_next) has_status = 2;
        this.privateData.page_size = data.page_size;
        this.privateData.page_num = data.page_num;

        return data.review_list.map((item) => {
          return {
            spuId: item.reviews_id,
            thumb: item.primary_image,
            title: item.reviews_title,
            avatar: item.reviews_avatar,
            author: item.reviews_author,
            mark: item.mark,
            issue: item.release_time,
            hot: item.is_hot,
            liked: item.is_liked,
            likerNum: item.liker_num,
          };
        });
      });

      this.setData({
        reviewsList: fresh ? nextList : this.data.reviewsList.concat(nextList),
        reviewsListLoadStatus: has_status,
      });

    } catch (err) {
      this.setData({ reviewsListLoadStatus: 3 });
    }
  },

  reviewListClickHandle(e) {
    const { index } = e.detail;
    const { spuId } = this.data.reviewsList[index];
    wx.navigateTo({
      url: `/pages/reviews/details/index?spuId=${spuId}`,
    });
  },

  // navToSearchPage() {
  //   wx.navigateTo({ url: '/pages/goods/search/index' });
  // },

  onSearchHandle() {
    let { keyword, defaultKeyword } = this.data;
    if (keyword === '') {
      keyword = defaultKeyword;
    }
    wx.navigateTo({
      url: `/pages/reviews/list/index?keyword=${keyword}`,
    });
    console.log(keyword);
  },

  onSearchChange(e) {
    let currentSearchValue = e.detail.value;
    this.setData({
      keyword: currentSearchValue
    })
  },

  onSearchClear() {
    this.setData({
      keyword: '',
    })
  }
});
